$(function () {
    //表格初始加载
    $('#grid_jkpz').datagrid({
        url: $.webPath() + "/base/jkpz/jkpzList",
        toolbar: "#toolbar_jkpz",
        fitColumns: true,
        pageNumber: 1,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'id', title: 'ID', hidden: true, width: 30},
                {field: 'bm', title: '接口名称', width: 30,
                    formatter: function (value, rowData) {
                        if (value == "cght")
                            return '采购合同提交仓储';
                        if (value == "xsd")
                            return '出库单放货仓储回传';

                    }},
                {field: 'flag', title: '启用状态', width: 35, formatter: function (value, rowData) {
                    if (value == "是")
                        return '已启用';
                    if (value == "否")
                        return '已停用';

                }},
            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
        }

    });

    $("#btn_jkpz_add").click(function () {
        jkpz_modify('新增接口配置', 'add_jkpz');
    });

    $("#btn_jkpz_start").click(function () {
        changeJkpzState();
    });

});


function jkpz_modify(title, action) {
    var href = $.webPath() + "/base/jkpz/jkpz_content";
    var actionUrl = $.webPath() + "/base/jkpz/" + action;
    jkpz_showDialog(title, href, actionUrl);
}

function jkpz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 820,
        height: 400,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_jkpz').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_jkpz').datagrid('reload');
                                $('#layout_jkpz').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//改变状态，启用停用
function changeJkpzState() {
    var record = $("#grid_jkpz").datagrid("getSelected");
    if (record) {
        $.messager.confirm("确认", "是启用或者停用", function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/base/jkpz/change_jkpz?jkpzId=" + record.id,
                    type: "json",
                    success: function (data) {
                        $('#grid_jkpz').datagrid('reload');
                    }
                });
            }
        })
    }
}